import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../bean/articles_bean.dart';
import '../controller/article_controller.dart';
import '../pages/login_page.dart';
import '../pages/web_page.dart';
import '../utils/user.dart';

///文章item自定义控件
class ArticleItem extends StatelessWidget {
  ArticleItem({super.key, required this.item});

  final ArticleBean item;

  final ArticleController articleCtr = Get.put(ArticleController());

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        child: Card(
          shadowColor: Colors.grey,
          color: Colors.white,
          // surfaceTintColor: Colors.white,
          margin: const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
          elevation: 3,
          child: Padding(
            padding: const EdgeInsets.all(10),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(children: [
                  showTopText(item.type == 1),
                  Text(
                    item.chapterName,
                  ),
                  const Spacer(),
                  Text(
                    item.niceDate,
                  )
                ]),
                const Padding(padding: EdgeInsets.only(top: 8)),
                Text(item.title,
                    style: const TextStyle(
                        fontWeight: FontWeight.w700, fontSize: 15)),
                const Padding(padding: EdgeInsets.only(top: 8)),
                Row(
                  children: [
                    Text(item.superChapterName),
                    const Spacer(),
                    collectBuild(item)
                  ],
                )
              ],
            ),
          ),
        ),
        onTap: () {
          Get.to(() => WebPage(), arguments: {"webUrl": item.link});
        });
  }

  ///构建收藏按钮
  Widget collectBuild(ArticleBean item) {
    var collect = item.collect.obs;
    return Obx(() => GestureDetector(
        child: Icon(Icons.star,
            color: collect.value ? Get.theme.colorScheme.primary : Colors.grey),
        onTap: () async {
          //如果登录状态，调用收藏接口，否则跳转到登录页面
          if (await User().isLogin()) {
            if (collect.value) {
              await articleCtr.postUnCollect(item.id);
              collect.value = false;
              item.collect = false;
            } else {
              articleCtr.postCollect(item.id);
              collect.value = true;
              item.collect = true;
            }
          } else {
            //跳转到登录页面
            Get.to(LoginPage());
          }
        }));
  }

  ///是否显示置顶按钮
  Widget showTopText(bool isShow) {
    if (!isShow) {
      return const SizedBox.shrink();
    }
    return Row(children: [
      Text(
        "置顶",
        style: TextStyle(
            color: Get.theme.colorScheme.primary, fontWeight: FontWeight.w700),
      ),
      const Padding(padding: EdgeInsets.only(left: 20)),
    ]);
  }
}
